package com.hg.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

/**
 * Created by admin on 2017/3/6.
 */

public class RenameButton extends Button {

    private Paint mPaint = new Paint();
    private Path mPath;

    private float padding = 5;
    private float b;
    private boolean isStart;

    private float X, Y;
    private int count;

    public RenameButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackground(null);
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(Color.argb(255, 65, 72, 75));
    }

    private static final String TAG = "RenameButton";

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        {
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(5);
            mPath = new Path();

            mPath.moveTo(getWidth() / 2, padding * 2);
            mPath.lineTo(padding, padding * 2);
            mPath.lineTo(padding, getHeight() - padding * 2);
            mPath.lineTo(getWidth() - padding, getHeight() - padding * 2);
            mPath.lineTo(getWidth() - padding, getHeight() / 2);

            canvas.drawPath(mPath, mPaint);

            int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);


            if (isStart) {

                if (count % 2 == 0){
                    X -= 0.6f;
                    Y += 0.6f;
                }else {

                    X += 0.6f;
                    Y -= 0.6f;
                }

                if (Math.abs(X) >= padding*2 || X >= 0) {
                    count ++;
                    if (count == 4){
                        X = 0;
                        Y = 0;
                        isStart = false;
                        count = 0;
                    }
                }
                canvas.translate(-padding + X, padding + Y);
                invalidate();
            } else {
                canvas.translate(-padding, padding);
            }

            canvas.rotate(45, getWidth() / 2, getHeight() / 2);
            mPath.reset();

            mPath.moveTo(getWidth() / 2 - padding * 2, padding / 2);
            mPath.lineTo(getWidth() / 2 + padding * 2, padding / 2);
            mPath.lineTo(getWidth() / 2 + padding * 2, getHeight() / 2);
            mPath.lineTo(getWidth() / 2, getHeight() / 2 + padding * 2);
            mPath.lineTo(getWidth() / 2 - padding * 2, getHeight() / 2);
            mPath.close();
            mPath.moveTo(getWidth() / 2 - padding * 2, padding * 2);
            mPath.lineTo(getWidth() / 2 + padding * 2, padding * 2);
            canvas.drawPath(mPath, mPaint);

            canvas.rotate(-45, getWidth() / 2, getHeight() / 2);
            canvas.translate(padding, -padding);
            canvas.restoreToCount(layerId);
//            if (isStart) {
//                mPaint.setStyle(Paint.Style.STROKE);
//                float c = (float) Math.sqrt(Math.pow(getWidth()/2,2)+Math.pow(getHeight()/2,2));
//                b += 2f;
//
//                if (b >= c*2) {
//                    finish();
//                    canvas.restoreToCount(layerId);
//                    return;
//                }
//
//                mPaint.setStrokeWidth(c*2 - b);
//
//                mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//                canvas.drawCircle(getWidth() / 2, getHeight() / 2, c, mPaint);
//                mPaint.setXfermode(null);
//
//                canvas.restoreToCount(layerId);
//                invalidate();
//            }
        }

    }


    public void click() {
        if (!isStart) {
            isStart = true;
            invalidate();
        }
    }

    public void finish() {
        isStart = false;
        b = 0;
    }
}
